package com.qipa.netty.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.ssl.SslContext;

public class MyClientInitializer extends ChannelInitializer<SocketChannel> {

    private final SslContext sslCtx;

    public MyClientInitializer(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        if (sslCtx != null) {
            pipeline.addLast(sslCtx.newHandler(ch.alloc()));
        }
        pipeline.addLast(new HttpClientCodec());/*HTTP 客户端的解码器*/
        pipeline.addLast(new HttpObjectAggregator(2048));/*HTTP 消息的合并处理*/
        pipeline.addLast(new MyClientHandler());
    }


}
